#ifndef __GLF_TASK_TASKMANAGER_IMPL_H_INCLUDED__
#define __GLF_TASK_TASKMANAGER_IMPL_H_INCLUDED__

namespace glf
{

inline bool 
TaskManager::HasPendingTask()
{
	return m_ConcurrentTasksQueue.Size() != 0 && !m_TasksPause;
}

inline int 
TaskManager::PendingTaskCount()
{
	return m_ConcurrentTasksQueue.Size();
}

inline void 
TaskManager::StartWork()
{
	glf::AtomicIncrement(m_WorkStatus);
}

inline void 
TaskManager::EndWork()
{
	glf::AtomicDecrement(m_WorkStatus);
}

inline bool 
TaskManager::IsWorking()
{
	return m_WorkStatus != 0;
}

inline void 
TaskManager::ForceImmediateTaskExecution(bool executeImmediately)
{
	m_ExecuteImmediately = executeImmediately;
}

inline bool 
TaskManager::AreTasksExecutedImmediately()
{
	return m_ExecuteImmediately;
}

inline void 
TaskManager::PauseTasksConsumption()
{
	m_TasksPause = true;
}

inline void 
TaskManager::ResumeTasksConsumption()
{
	m_TasksPause = false;
	Signal();
}

template<typename Tag>
TaskManager& 
TaskManager::GetInstance()
{
	static TaskManager* taskManagerInstance = NULL;
	static int lock = 0;

	if(!taskManagerInstance)
	{
		while(AtomicCompareAndSwap(lock, 1, 0) != 0) {Thread::Sleep(1);}
		if(!taskManagerInstance)
		{
			TaskManager* temp = new TaskManager;
			Membar();
			taskManagerInstance = temp;
		}
		lock = 0;
	}

	return *taskManagerInstance;
}

} // namespace glf

#endif // __GLF_TASK_TASKMANAGER_IMPL_H_INCLUDED__